<template>
  <div class="search">
    <div class="search-input-wrapper">
      <SearchInput v-model="query"></SearchInput>
    </div>
    <scroll
      ref="scrollRef"
      class="search-content"
      v-show="!query"
    >
      <div>
        <div class="hot-keys">
          <h1 class="title">热门搜索</h1>
          <ul>
            <li
              class="item"
              v-for="item in hotKeys"
              :key="item.id"
              @click="() => {addQuery(item.key)}"
            >
              <span>{{item.key}}</span>
            </li>
          </ul>
        </div>
        <div class="search-history" v-show="searchHistory.length">
          <h1 class="title">
            <span class="text">搜索历史</span>
            <span class="clear" @click="showConfirm">
              <i class="icon-clear"></i>
            </span>
          </h1>
          <confirm
            ref="confirmRef"
            text="是否清空所有搜索历史"
            confirm-btn-text="清空"
            @confirm="clearSearch"
          >
          </confirm>
          <search-list
            :searches="searchHistory"
            @select="addQuery"
            @delete="deleteSearch"
          ></search-list>
        </div>
      </div>
    </scroll>
    <div class="search-result" v-show="query">
      <suggest
        :query="query"
        @select-song="selectSong"
        @select-singer="selectSinger"
      ></suggest>
    </div>
    <router-view v-slot="{ Component }">
      <transition appear name="slide">
        <component :is="Component" :data="selectedSinger"/>
      </transition>
    </router-view>
  </div>
</template>

<script>
import { ref, watch, nextTick } from 'vue'
import { useStore } from 'vuex'
import { useRouter } from 'vue-router'
import SearchInput from '../../components/search/SearchInput'
import Scroll from '../../components/wrapScroll/index'
import Confirm from '../../components/base/comfirm/Confirm'
import Suggest from '../../components/search/Suggest'
import SearchList from '../../components/base/searchList/SearchList'
import { getHotKeys } from '../../service/search'
import { SINGER_KEY } from '../../assets/js/constant'
import storage from 'good-storage'
import useSearchHistory from '../../components/search/use-search-history'

// 弹窗提示
const useConfirmEffect = () => {
  const confirmRef = ref(null)
  const showConfirm = () => {
    confirmRef.value.show()
  }
  return { confirmRef, showConfirm }
}

// 热门关键字
const useHotKeysEffect = () => {
  const hotKeys = ref([])
  getHotKeys().then(res => {
    hotKeys.value = res.hotKeys
  })
  return { hotKeys }
}

// 搜索选择
const useSearchQueryEffect = (saveSearch) => {
  const store = useStore()
  const router = useRouter()
  const scrollRef = ref(null)
  const selectedSinger = ref(null)
  const query = ref('')

  watch(query, async (newQuery) => {
    if (!newQuery) {
      await nextTick()
      refreshScroll()
    }
  })

  const addQuery = (key) => {
    query.value = key
  }

  const selectSong = (song) => {
    saveSearch(query.value)
    store.dispatch('addSong', song)
  }
  const selectSinger = (singer) => {
    saveSearch(query.value)
    selectedSinger.value = singer
    cacheSinger(singer)
    router.push({
      path: `/search/${singer.mid}`
    })
  }
  return {
    scrollRef,
    selectedSinger,
    query,
    addQuery,
    selectSong,
    selectSinger
  }
  function cacheSinger (singer) {
    storage.session.set(SINGER_KEY, singer)
  }

  function refreshScroll () {
    scrollRef.value.scroll.refresh()
  }
}

export default {
  name: 'Search',
  setup () {
    const {
      searchHistory, saveSearch, deleteSearch, clearSearch
    } = useSearchHistory() // 弹窗提示

    const { confirmRef, showConfirm } = useConfirmEffect() // 热门关键字

    const { hotKeys } = useHotKeysEffect() // 搜索选择

    const {
      scrollRef, selectedSinger, query,
      addQuery, selectSong, selectSinger
    } = useSearchQueryEffect(saveSearch)

    return {
      query,
      hotKeys,
      searchHistory,
      scrollRef,
      addQuery,
      selectSong,
      selectSinger,
      deleteSearch,
      selectedSinger,
      confirmRef,
      showConfirm,
      clearSearch
    }
  },
  components: {
    Scroll, Confirm, SearchInput, Suggest, SearchList
  }
}
</script>

<style lang="scss" scoped>
  .search {
    position: fixed;
    width: 100%;
    top: 88px;
    bottom: 0;
    display: flex;
    flex-direction: column;
    .search-input-wrapper {
      margin: 20px;
    }
    .search-content {
      flex: 1;
      overflow: hidden;
      .hot-keys {
        margin: 0 20px 20px 20px;
        .title {
          margin-bottom: 20px;
          font-size: $font-size-medium;
          color: $color-text-l;
        }
        .item {
          display: inline-block;
          padding: 5px 10px;
          margin: 0 20px 10px 0;
          border-radius: 6px;
          background: $color-highlight-background;
          font-size: $font-size-medium;
          color: $color-text-d;
        }
      }
      .search-history {
        position: relative;
        margin: 0 20px;
        .title {
          display: flex;
          align-items: center;
          height: 40px;
          font-size: $font-size-medium;
          color: $color-text-l;
          .text {
            flex: 1;
          }
          .clear {
            @include extend-click();
            .icon-clear {
              font-size: $font-size-medium;
              color: $color-text-d;
            }
          }
        }
      }
    }
    .search-result {
      flex: 1;
      overflow: hidden;
    }
  }
</style>
